package com.ybear.ybutils.utils.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import androidx.annotation.StyleRes;

import com.ybear.ybutils.utils.Utils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import static android.graphics.drawable.GradientDrawable.LINE;
import static android.graphics.drawable.GradientDrawable.OVAL;
import static android.graphics.drawable.GradientDrawable.RECTANGLE;
import static android.graphics.drawable.GradientDrawable.RING;

public class WindowStyle {
    @IntDef({ RECTANGLE, OVAL, LINE, RING })
    @Retention(RetentionPolicy.SOURCE)
    public @interface Shape {}

    private final Context mContext;
    @DrawableRes
    private int mBackgroundDrawableResource = 0;
    private GradientDrawable mBackgroundDrawable;
    private float dimAmount = -1F;
    private int gravity = Gravity.CENTER;
    private boolean isSystemDialog;
    private int radius = -1;
    @StyleRes
    private int animResId = 0;

    public WindowStyle(Context context) {
        mContext = context;
        mBackgroundDrawable = new GradientDrawable();
        setShape( RECTANGLE );
        setBackgroundColor( Color.WHITE );
    }

    @DrawableRes
    public int getBackgroundDrawableResource() {
        return mBackgroundDrawableResource;
    }
    public WindowStyle setBackgroundDrawableResource(@DrawableRes int res) {
        mBackgroundDrawableResource = res;
        return this;
    }
    public GradientDrawable getBackgroundDrawable() {
        new GradientDrawable();
        return mBackgroundDrawable;
    }
    public WindowStyle setBackgroundDrawable(Drawable mBackgroundDrawable) {
        this.mBackgroundDrawable = (GradientDrawable) mBackgroundDrawable;
        return this;
    }
    public WindowStyle setBackgroundColor(@ColorInt int color) {
        mBackgroundDrawable.setColor( color );
        return this;
    }

    public int getCornerRadius() { return radius; }
    public WindowStyle setCornerRadius(int radius) {
        this.radius = Utils.dp2Px( mContext, radius );
        mBackgroundDrawable.setCornerRadius( this.radius );
        return this;
    }
    public WindowStyle setCornerRadii(float[] radii) {
        mBackgroundDrawable.setCornerRadii( radii );

        return this;
    }

    public float getDimAmount() { return dimAmount; }
    public WindowStyle setDimAmount(@FloatRange(from = 0F, to = 1F) float dimAmount) {
        this.dimAmount = dimAmount;
        return this;
    }

    public int getGravity() { return gravity; }
    public void setGravity(int gravity) { this.gravity = gravity; }

    public void setStroke(int width, @ColorInt int color) {
        mBackgroundDrawable.setStroke( width, color );
    }
    
    public void setShape(@Shape int shape) { mBackgroundDrawable.setShape( shape ); }

    public boolean isSystemDialog() {
        return isSystemDialog;
    }

    public WindowStyle setSystemDialog(boolean systemDialog) {
        isSystemDialog = systemDialog;
        return this;
    }

    public int getAnimations() { return animResId; }
    public WindowStyle setAnimations(int resId) {
        animResId = resId;
        return this;
    }
}